//
//  PaperRecordViewController.swift
//  Exam-iOS
//
//  Created by lishengfeng on 2018/3/24.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import RxDataSources
import RxSwift
import RxCocoa
import SnapKit
import Then

class PaperRecordViewController: ViewController, Refreshable, LeftBarButtonItemable, NullData {
    var vm = PaperRecordViewModel()
    var dataSourcee: RxTableViewSectionedReloadDataSource<PaperRecordSection>!
    var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        bindUI()
        
        tableView.mj_header.beginRefreshing()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        setPopRecognizerEnable(true)
    }
    
    func initUI() {
        setLeftItem()
        title = "答题记录"
        view.backgroundColor = UIColor.lsf_color16(0xF9F9F9)
        
        tableView = UITableView.init(frame: CGRect.zero, style: .plain).then({ (tableView) in
            tableView.register(PaperRecordCell.classForCoder(), forCellReuseIdentifier: "PaperRecordCell")
            tableView.tableFooterView = UIView()
            tableView.backgroundColor = UIColor.lsf_color16(0xF9F9F9)
            tableView.separatorStyle = .none
            
            self.view.addSubview(tableView)
            tableView.snp.makeConstraints({ (maker) in
                maker.edges.equalTo(safeAreaInsets())
            })
        })
    }
    
    func bindUI() {
        dataSourcee = RxTableViewSectionedReloadDataSource.init(configureCell: { (ds, tv, ip, item) -> UITableViewCell in
            let cell = tv.dequeueReusableCell(withIdentifier: "PaperRecordCell", for: ip) as! PaperRecordCell
            cell.set(title: item.paperTitle, score: item.totalScore, date: item.submittedTime)
            return cell
        })
        
        tableView.rx.modelSelected(PaperRecordModel.self).subscribe(onNext: { [weak self] (model) in
            
            guard let paperId = model.paperId else { return }
            let paperVC = PaperViewController.startPaper(paperId, status: PaperStatus.record, title: model.paperTitle, pUserSummaryId: model.pUserSummaryId)
            self?.navigationController?.pushViewController(paperVC, animated: true)
            
        }).disposed(by: disposeBag)
        
        tableView.rx.itemSelected.subscribe(onNext: { [weak self] (indexPath) in
            self?.tableView.deselectRow(at: indexPath, animated: true)
        }).disposed(by: disposeBag)
        
        vm.vmData.asObservable().bind(to: tableView.rx.items(dataSource: dataSourcee)).disposed(by: disposeBag)
        
        initRefresh(tableView, headerBlock: {
            self.vm.requestCommand.onNext(true)
        }) {
            self.vm.requestCommand.onNext(false)
        }
        
        vm.autoSetRefreshStatus(header: tableView.mj_header, footer: tableView.mj_footer).disposed(by: disposeBag)
        
        ///空数据处理
        vm.isNull.asObservable().subscribe(onNext: { [weak self] (isNull) in
            if isNull {
                let nilView = self?.nilView(text: "还没有答题记录哟~")
                self?.tableView.backgroundView = nilView
            }
            else {
                self?.tableView.backgroundView = nil
            }
        }).disposed(by: disposeBag)
    }
}
